package com.ecoolex.hotel.saas.admin.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.hotel.saas.admin.core.AdminConstant;
import com.ecoolex.hotel.saas.entity.Activity;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.pojo.AddActivityDTO;
import com.ecoolex.hotel.saas.pojo.EditActivityDTO;
import com.ecoolex.hotel.saas.ws.ActivityService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "商圈活动管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
        key = ActivityGraphql.AUTH,
        name = "商圈活动管理",
        remark = "商圈活动管理")
public class ActivityGraphql {

    public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-ACTIVITY";

    private static final String AUTH_GET = AUTH + "-GET";

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    private static final String AUTH_ADD = AUTH + "-ADD";

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    private static final String AUTH_DEL = AUTH + "-DEL";

    @Reference
    private ActivityService activityService;

    @TypeField(type = Activity.class, field = "activityStoreNumber", description = "关联的商户数量")
    public Integer activityStoreNumber(@Source Activity source){
        return activityService.activityStoreNumber(source.getActivityId());
    }

    @QueryField(field = "activity", description = "商圈活动详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商圈活动详情")
    public Activity get(@NonNull Integer activityId, AdminMemberAuthority user){
        user.assertAuth(AUTH_GET);
        return activityService.get(activityId);
    }

    @QueryField(field = "activityList", description = "商圈活动列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "商圈活动列表")
    public Pagination<Activity> query(String name, @NonNull Page page, AdminMemberAuthority user){
        user.assertAuth(AUTH_QUERY);
        return  activityService.query(name,page);
    }

    @MutationField(field = "addActivity", description = "添加商圈活动")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商圈活动")
    public Activity add(@NonNull AddActivityDTO add, AdminMemberAuthority user){
        user.assertAuth(AUTH_ADD);
        return activityService.add(add,user.userName());
    }

    @MutationField(field = "editActivity", description = "编辑商圈活动")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商圈活动")
    public Activity edit(@NonNull EditActivityDTO edit, AdminMemberAuthority user){
        user.assertAuth(AUTH_EDIT);
        Activity old = activityService.get(edit.getActivityId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商圈活动不存在");
        BeanUtil.copy(edit, old);
        return activityService.edit(old,user.userName());
    }

    @MutationField(field = "delActivity", description = "删除商圈活动")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商圈活动")
    public Boolean delete(@NonNull Integer activityId, AdminMemberAuthority user){
        user.assertAuth(AUTH_DEL);
        Activity old = activityService.get(activityId);
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商圈活动不存在");
        activityService.delete(old,user.userName());
        return true;
    }
}
